package com.ruoyi.web.controller.common;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.minio.util.FileUploadMinioUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author alison
 * @Date 2024/12/15 16:24
 * @Version 1.0
 * @Description
 */
@ConditionalOnProperty(prefix = "minio", name = "enabled", havingValue = "true")
@Controller
@RequestMapping("/oss")
public class OssController {

    @PostMapping("/uploadMinio")
    @ResponseBody
    public AjaxResult uploadFileMinio(@RequestParam("file") MultipartFile file) throws Exception {
        try {
            // 上传并返回新文件名称
            String fileName = FileUploadMinioUtils.uploadMinio(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", fileName);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
